import {
	getAction,
	postAction,
	postFormAction
} from "@/utils/request.js";

import {
	getParams
} from "./form_js_utils.js";

export function launchJs(script) {
	// 小程序中不能用eval
	// eval(script);
	console.log('script', script);

	let k = script.indexOf('getTaskForMe');
	if (k != -1) {
		let params = getParams('getTaskForMe', script);
		getTaskForMe(params);
	}
}

function reload() {
	uni.$emit('updatePage', {});
	return
}

function getTaskForMe(id) {
	console.log('getTaskForMe', id);
	uni.showModal({
		title: "提示",
		content: "您确定要领取么",
		success: (res) => {
			if (res.confirm) {
				let formulaStr = "#getTask(" + id + ")";
				let params = {
					formula: formulaStr,
				};
				uni.showLoading({
					title: '加载中'
				});
				postFormAction('/visual/formula/doFormula', params).then((
						res) => {
						uni.hideLoading();
						if (res.ret == '1') {
							let json = JSON.parse(res.value);
							uni.showToast({
								icon: "none",
								title: json.msg,
							});
							// 刷新页面
							// reload();
						} else {
							uni.showToast({
								icon: "none",
								title: res.msg,
							});
						}
					})
					.catch((e) => {
						uni.showToast({
							icon: "none",
							title: e.message,
						});
					});
			} else if (res.cancel) {
				console.log("用户点击取消");
			}
		},
	});
}